package com.itheima.service.lpml;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.entiy.Category;
import com.itheima.entiy.Dish;
import com.itheima.entiy.Setmeal;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService{

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {

        //添加查询条件，根据id分类进行查询菜品数据
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,id);

        int count = dishService.count(wrapper);

        if(count>0){
            throw new CustomException("当前分类下关联了菜品，希望你小心点");

        }


        //查询套餐
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Setmeal::getCategoryId,id);

        int count1 = setmealService.count(queryWrapper);

        if(count1>0){
            throw new CustomException("当前套餐下关联了菜品，希望你小心点");


        }


        removeById(id);
    }












}
